package com.xiaochen.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaochen.manager.mapper.ProductDetailsMapper;
import com.xiaochen.manager.mapper.ProductMapper;
import com.xiaochen.manager.mapper.ProductSkuMapper;
import com.xiaochen.manager.service.ProductService;
import com.xiaochen.model.dto.product.ProductDto;
import com.xiaochen.model.entity.product.Product;
import com.xiaochen.model.entity.product.ProductDetails;
import com.xiaochen.model.entity.product.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 商品管理
 * @author pinkhors
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    /**
     * 分页查询
     */
    @Override
    public PageInfo<Product> selectByPage(ProductDto productDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productMapper.selectByPage(productDto);
        return new PageInfo<>(list);
    }

    /**
     * 添加
     */
    @Transactional
    @Override
    public void saveProduct(Product product) {
        // 1.保存商品的基本信息到 product 表
        product.setAuditStatus(0);
        product.setStatus(0);
        productMapper.saveProduct(product);

        // 2.获取商品sku列表集合,保存sku信息到 productSku 表
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList){
            productSku.setSkuCode(UUID.randomUUID().toString());
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName()+productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);

            productSkuMapper.save(productSku);
        }

        // 3.保存商品详情到 productDetails 表
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
    }

    /**
     * 根据商品ID查询商品信息
     */
    @Override
    public Product selectById(Long id) {
        // 1.根据ID查询product的基本信息
        Product product = productMapper.selectById(id);

        // 2.根据ID查询商品的sku信息
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkuList);

        // 3.根据ID查询商品的productDetails信息
        ProductDetails productDetails = productDetailsMapper.selectByProductId(id);
        String imageUrls = productDetails.getImageUrls();
        product.setDetailsImageUrls(imageUrls);

        return product;
    }

    /**
     * 保存修改的数据
     */
    @Transactional
    @Override
    public void updateById(Product product) {
        // 1.修改product
        productMapper.updateById(product);
        // 2.修改product_sku
        // 2.1根据product获取需要修改的product_sku的信息
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList){
            productSkuMapper.updateById(productSku);
        }
        // 3.修改product_details表
        String detailsImageUrls = product.getDetailsImageUrls();
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(detailsImageUrls);
        productDetailsMapper.updateById(productDetails);


    }
/// ////////////////////////////////////////

    /**
     * 根据ID删除
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        // 1.根据ID删除product
        productMapper.deleteById(id);
        // 2.根据ProductID删除productSku
        productSkuMapper.deleteByProductId(id);
        // 3.根据productID删除productDetails
        productDetailsMapper.deleteByProductId(id);
    }
////////////////////////////////////////////////////

    /**
     * 审核
     * @param id
     * @param auditStatus
     */
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if(auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    /**
     * 商品的上下架
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }
}
